#include "WaveGen.h"
#include "dac.h"
#include "tim.h"
#include "stm32g0xx_ll_tim.h"

volatile uint32_t WaveGenDitherTick[OSCILLATOR_MAX_NUM];
volatile uint32_t WaveGenSwitchTick[OSCILLATOR_MAX_NUM];

const uint16_t WaveSinConst[WAVE_SAMPLE_NUM] = {
0,0,0,0,0,1,1,1,2,3,4,4,5,6,7,9,10,11,13,14,16,17,19,21,23,25,27,29,31,33,36,38,41,43,46,49,52,55,58,61,64,67,70,74,77,81,84,88,92,96,100,104,108,112,116,121,125,129,134,139,143,148,153,158,163,168,173,178,184,189,194,200,206,211,217,223,229,235,241,247,253,259,265,272,278,285,291,298,305,311,
318,325,332,339,346,354,361,368,376,383,391,398,406,414,421,429,437,445,453,461,469,478,486,494,503,511,520,528,537,546,555,563,572,581,590,599,608,618,627,636,646,655,664,674,684,693,703,713,722,732,742,752,762,772,782,792,802,813,823,833,844,854,865,875,886,896,907,918,928,939,950,961,972,983,994,1005,1016,1027,1038,1050,1061,1072,1083,1095,1106,1118,1129,1141,1152,1164,1176,1187,1199,1211,1222,1234,1246,1258,1270,1282,
1294,1306,1318,1330,1342,1354,1366,1378,1390,1402,1415,1427,1439,1451,1464,1476,1488,1501,1513,1526,1538,1551,1563,1576,1588,1601,1613,1626,1638,1651,1664,1676,1689,1702,1714,1727,1740,1753,1765,1778,1791,1804,1816,1829,1842,1855,1868,1880,1893,1906,1919,1932,1945,1957,1970,1983,1996,2009,2022,2035,2048,2060,2073,2086,2099,2112,2125,2138,2150,2163,2176,2189,2202,2215,2227,2240,2253,2266,2279,2291,2304,2317,2330,2342,2355,2368,2381,2393,2406,2419,2431,2444,2457,2469,2482,2494,2507,2519,2532,2544,
2557,2569,2582,2594,2607,2619,2631,2644,2656,2668,2680,2693,2705,2717,2729,2741,2753,2765,2777,2789,2801,2813,2825,2837,2849,2861,2873,2884,2896,2908,2919,2931,2943,2954,2966,2977,2989,3000,3012,3023,3034,3045,3057,3068,3079,3090,3101,3112,3123,3134,3145,3156,3167,3177,3188,3199,3209,3220,3230,3241,3251,3262,3272,3282,3293,3303,3313,3323,3333,3343,3353,3363,3373,3382,3392,3402,3411,3421,3431,3440,3449,3459,3468,3477,3487,3496,3505,3514,3523,3532,3540,3549,3558,3567,3575,3584,3592,3601,3609,3617,
3626,3634,3642,3650,3658,3666,3674,3681,3689,3697,3704,3712,3719,3727,3734,3741,3749,3756,3763,3770,3777,3784,3790,3797,3804,3810,3817,3823,3830,3836,3842,3848,3854,3860,3866,3872,3878,3884,3889,3895,3901,3906,3911,3917,3922,3927,3932,3937,3942,3947,3952,3956,3961,3966,3970,3974,3979,3983,3987,3991,3995,3999,4003,4007,4011,4014,4018,4021,4025,4028,4031,4034,4037,4040,4043,4046,4049,4052,4054,4057,4059,4062,4064,4066,4068,4070,4072,4074,4076,4078,4079,4081,4082,4084,4085,4086,4088,4089,4090,4091,
4091,4092,4093,4094,4094,4094,4095,4095,4095,4095,4096,4095,4095,4095,4095,4094,4094,4094,4093,4092,4091,4091,4090,4089,4088,4086,4085,4084,4082,4081,4079,4078,4076,4074,4072,4070,4068,4066,4064,4062,4059,4057,4054,4052,4049,4046,4043,4040,4037,4034,4031,4028,4025,4021,4018,4014,4011,4007,4003,3999,3995,3991,3987,3983,3979,3974,3970,3966,3961,3956,3952,3947,3942,3937,3932,3927,3922,3917,3911,3906,3901,3895,3889,3884,3878,3872,3866,3860,3854,3848,3842,3836,3830,3823,3817,3810,3804,3797,3790,3784,
3777,3770,3763,3756,3749,3741,3734,3727,3719,3712,3704,3697,3689,3681,3674,3666,3658,3650,3642,3634,3626,3617,3609,3601,3592,3584,3575,3567,3558,3549,3540,3532,3523,3514,3505,3496,3487,3477,3468,3459,3449,3440,3431,3421,3411,3402,3392,3382,3373,3363,3353,3343,3333,3323,3313,3303,3293,3282,3272,3262,3251,3241,3230,3220,3209,3199,3188,3177,3167,3156,3145,3134,3123,3112,3101,3090,3079,3068,3057,3045,3034,3023,3012,3000,2989,2977,2966,2954,2943,2931,2919,2908,2896,2884,2873,2861,2849,2837,2825,2813,
2801,2789,2777,2765,2753,2741,2729,2717,2705,2693,2680,2668,2656,2644,2631,2619,2607,2594,2582,2569,2557,2544,2532,2519,2507,2494,2482,2469,2457,2444,2431,2419,2406,2393,2381,2368,2355,2342,2330,2317,2304,2291,2279,2266,2253,2240,2227,2215,2202,2189,2176,2163,2150,2138,2125,2112,2099,2086,2073,2060,2048,2035,2022,2009,1996,1983,1970,1957,1945,1932,1919,1906,1893,1880,1868,1855,1842,1829,1816,1804,1791,1778,1765,1753,1740,1727,1714,1702,1689,1676,1664,1651,1638,1626,1613,1601,1588,1576,1563,1551,
1538,1526,1513,1501,1488,1476,1464,1451,1439,1427,1415,1402,1390,1378,1366,1354,1342,1330,1318,1306,1294,1282,1270,1258,1246,1234,1222,1211,1199,1187,1176,1164,1152,1141,1129,1118,1106,1095,1083,1072,1061,1050,1038,1027,1016,1005,994,983,972,961,950,939,928,918,907,896,886,875,865,854,844,833,823,813,802,792,782,772,762,752,742,732,722,713,703,693,684,674,664,655,646,636,627,618,608,599,590,581,572,563,555,546,537,528,520,511,503,494,486,478,
469,461,453,445,437,429,421,414,406,398,391,383,376,368,361,354,346,339,332,325,318,311,305,298,291,285,278,272,265,259,253,247,241,235,229,223,217,211,206,200,194,189,184,178,173,168,163,158,153,148,143,139,134,129,125,121,116,112,108,104,100,96,92,88,84,81,77,74,70,67,64,61,58,55,52,49,46,43,41,38,36,33,31,29,27,25,23,21,19,17,16,14,13,11,10,9,7,6,5,4,
4,3,2,1,1,1,0,0,0,0,
};

const uint16_t WaveTriangleConst[WAVE_SAMPLE_NUM] = {
0,8,16,24,32,40,49,57,65,73,81,90,98,106,114,122,131,139,147,155,163,171,180,188,196,204,212,221,229,237,245,253,262,270,278,286,294,303,311,319,327,335,343,352,360,368,376,384,393,401,409,417,425,434,442,450,458,466,475,483,491,499,507,515,524,532,540,548,556,565,573,581,589,597,606,614,622,630,638,647,655,663,671,679,687,696,704,712,720,728,737,745,753,761,769,778,786,794,802,810,
819,827,835,843,851,859,868,876,884,892,900,909,917,925,933,941,950,958,966,974,982,990,999,1007,1015,1023,1031,1040,1048,1056,1064,1072,1081,1089,1097,1105,1113,1122,1130,1138,1146,1154,1162,1171,1179,1187,1195,1203,1212,1220,1228,1236,1244,1253,1261,1269,1277,1285,1294,1302,1310,1318,1326,1334,1343,1351,1359,1367,1375,1384,1392,1400,1408,1416,1425,1433,1441,1449,1457,1466,1474,1482,1490,1498,1506,1515,1523,1531,1539,1547,1556,1564,1572,1580,1588,1597,1605,1613,1621,1629,
1638,1646,1654,1662,1670,1678,1687,1695,1703,1711,1719,1728,1736,1744,1752,1760,1769,1777,1785,1793,1801,1809,1818,1826,1834,1842,1850,1859,1867,1875,1883,1891,1900,1908,1916,1924,1932,1941,1949,1957,1965,1973,1981,1990,1998,2006,2014,2022,2031,2039,2047,2055,2063,2072,2080,2088,2096,2104,2113,2121,2129,2137,2145,2153,2162,2170,2178,2186,2194,2203,2211,2219,2227,2235,2244,2252,2260,2268,2276,2285,2293,2301,2309,2317,2325,2334,2342,2350,2358,2366,2375,2383,2391,2399,2407,2416,2424,2432,2440,2448,
2457,2465,2473,2481,2489,2497,2506,2514,2522,2530,2538,2547,2555,2563,2571,2579,2588,2596,2604,2612,2620,2628,2637,2645,2653,2661,2669,2678,2686,2694,2702,2710,2719,2727,2735,2743,2751,2760,2768,2776,2784,2792,2800,2809,2817,2825,2833,2841,2850,2858,2866,2874,2882,2891,2899,2907,2915,2923,2932,2940,2948,2956,2964,2972,2981,2989,2997,3005,3013,3022,3030,3038,3046,3054,3063,3071,3079,3087,3095,3104,3112,3120,3128,3136,3144,3153,3161,3169,3177,3185,3194,3202,3210,3218,3226,3235,3243,3251,3259,3267,
3276,3284,3292,3300,3308,3316,3325,3333,3341,3349,3357,3366,3374,3382,3390,3398,3407,3415,3423,3431,3439,3447,3456,3464,3472,3480,3488,3497,3505,3513,3521,3529,3538,3546,3554,3562,3570,3579,3587,3595,3603,3611,3619,3628,3636,3644,3652,3660,3669,3677,3685,3693,3701,3710,3718,3726,3734,3742,3751,3759,3767,3775,3783,3791,3800,3808,3816,3824,3832,3841,3849,3857,3865,3873,3882,3890,3898,3906,3914,3923,3931,3939,3947,3955,3963,3972,3980,3988,3996,4004,4013,4021,4029,4037,4045,4054,4062,4070,4078,4086,
4095,4086,4078,4070,4062,4054,4045,4037,4029,4021,4013,4004,3996,3988,3980,3972,3963,3955,3947,3939,3931,3923,3914,3906,3898,3890,3882,3873,3865,3857,3849,3841,3832,3824,3816,3808,3800,3791,3783,3775,3767,3759,3751,3742,3734,3726,3718,3710,3701,3693,3685,3677,3669,3660,3652,3644,3636,3628,3619,3611,3603,3595,3587,3579,3570,3562,3554,3546,3538,3529,3521,3513,3505,3497,3488,3480,3472,3464,3456,3447,3439,3431,3423,3415,3407,3398,3390,3382,3374,3366,3357,3349,3341,3333,3325,3316,3308,3300,3292,3284,
3276,3267,3259,3251,3243,3235,3226,3218,3210,3202,3194,3185,3177,3169,3161,3153,3144,3136,3128,3120,3112,3104,3095,3087,3079,3071,3063,3054,3046,3038,3030,3022,3013,3005,2997,2989,2981,2972,2964,2956,2948,2940,2932,2923,2915,2907,2899,2891,2882,2874,2866,2858,2850,2841,2833,2825,2817,2809,2800,2792,2784,2776,2768,2760,2751,2743,2735,2727,2719,2710,2702,2694,2686,2678,2669,2661,2653,2645,2637,2628,2620,2612,2604,2596,2588,2579,2571,2563,2555,2547,2538,2530,2522,2514,2506,2497,2489,2481,2473,2465,
2457,2448,2440,2432,2424,2416,2407,2399,2391,2383,2375,2366,2358,2350,2342,2334,2325,2317,2309,2301,2293,2285,2276,2268,2260,2252,2244,2235,2227,2219,2211,2203,2194,2186,2178,2170,2162,2153,2145,2137,2129,2121,2113,2104,2096,2088,2080,2072,2063,2055,2047,2039,2031,2022,2014,2006,1998,1990,1981,1973,1965,1957,1949,1941,1932,1924,1916,1908,1900,1891,1883,1875,1867,1859,1850,1842,1834,1826,1818,1809,1801,1793,1785,1777,1769,1760,1752,1744,1736,1728,1719,1711,1703,1695,1687,1678,1670,1662,1654,1646,
1638,1629,1621,1613,1605,1597,1588,1580,1572,1564,1556,1547,1539,1531,1523,1515,1506,1498,1490,1482,1474,1466,1457,1449,1441,1433,1425,1416,1408,1400,1392,1384,1375,1367,1359,1351,1343,1334,1326,1318,1310,1302,1294,1285,1277,1269,1261,1253,1244,1236,1228,1220,1212,1203,1195,1187,1179,1171,1162,1154,1146,1138,1130,1122,1113,1105,1097,1089,1081,1072,1064,1056,1048,1040,1031,1023,1015,1007,999,990,982,974,966,958,950,941,933,925,917,909,900,892,884,876,868,859,851,843,835,827,
819,810,802,794,786,778,769,761,753,745,737,728,720,712,704,696,687,679,671,663,655,647,638,630,622,614,606,597,589,581,573,565,556,548,540,532,524,515,507,499,491,483,475,466,458,450,442,434,425,417,409,401,393,384,376,368,360,352,343,335,327,319,311,303,294,286,278,270,262,253,245,237,229,221,212,204,196,188,180,171,163,155,147,139,131,122,114,106,98,90,81,73,65,57,49,40,32,24,16,8,

};

const uint16_t WaveSquareConst[WAVE_SAMPLE_NUM] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,
4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,4095,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};

uint16_t WaveOutBuf[OSCILLATOR_MAX_NUM][WAVE_SAMPLE_NUM];

uint16_t ToneTab[44] = {
    TONE_A2, TONE_AS2, TONE_B2, TONE_C3, TONE_CS3, TONE_D3, TONE_DS3, TONE_E3, TONE_F3, TONE_FS3, TONE_G3, TONE_GS3,
    TONE_A3, TONE_AS3, TONE_B3, TONE_C4, TONE_CS4, TONE_D4, TONE_DS4, TONE_E4, TONE_F4, TONE_FS4, TONE_G4, TONE_GS4,
    TONE_A4, TONE_AS4, TONE_B4, TONE_C5, TONE_CS5, TONE_D5, TONE_DS5, TONE_E5, TONE_F5, TONE_FS5, TONE_G5, TONE_GS5,
    TONE_A5, TONE_AS5, TONE_B5, TONE_C6, TONE_CS6, TONE_D6, TONE_DS6, TONE_E6};

float DitherCoefficient[DITHER_COEFFICIENT_NUM] = {1.00, 1.02, 1.04, 1.02, 1.0, 0.98, 0.96, 0.98};

uint16_t* WaveTimbre[TIMBRE_MAX_NUM] = {&WaveSinConst, &WaveTriangleConst, &WaveSquareConst, &WaveSquareConst};
TIM_HandleTypeDef *TimHandle[OSCILLATOR_MAX_NUM] = {&htim1, &htim3};
DAC_HandleTypeDef *DacHandle[OSCILLATOR_MAX_NUM] = {&hdac1, &hdac1};
uint16_t DacPin[OSCILLATOR_MAX_NUM] = {GPIO_PIN_4, GPIO_PIN_5};
uint32_t DacChannel[OSCILLATOR_MAX_NUM] = {DAC_CHANNEL_1, DAC_CHANNEL_2};
uint32_t TimBaseAddr[OSCILLATOR_MAX_NUM] = {TIM1_BASE, TIM3_BASE};

uint8_t WaveTimbreFlag[OSCILLATOR_MAX_NUM];
uint8_t WaveDitherFlag[OSCILLATOR_MAX_NUM];
uint8_t WavePitchFlag[OSCILLATOR_MAX_NUM];
uint16_t WavePitchTickBackup[OSCILLATOR_MAX_NUM];
uint16_t WavePitchTickNext[OSCILLATOR_MAX_NUM];
uint8_t WaveDitherCount[OSCILLATOR_MAX_NUM];
uint8_t WaveVolumeCount[OSCILLATOR_MAX_NUM];

void WaveGenInit(void)
{
  uint16_t i, j;
  uint16_t *WaveTimbreData;
  for (i = 0; i < OSCILLATOR_MAX_NUM; i++)
  {
    WaveTimbreFlag[i] = TIMBRE_DEFAULT_VALUE;
    WaveDitherFlag[i] = DITHER_DEFAULT_VALUE;
    WavePitchFlag[i] = PITCH_DEFAULT_VALUE;
    WaveGenDitherTick[i] = HAL_GetTick();
    WaveGenSwitchTick[i] = HAL_GetTick();
    WavePitchTickBackup[i] = 0xFFFF;
    WavePitchTickNext[i] = 0xFFFF;
  }

  for (j = 0; j < OSCILLATOR_MAX_NUM; j++)
  {
    WaveTimbreData = WaveTimbre[WaveTimbreFlag[j]];
    for (i = 0; i < WAVE_SAMPLE_NUM; i++)
    {
      WaveOutBuf[j][i] = WaveTimbreData[i] / (VOLUME_MAX_NUM - WaveVolumeCount[j]);
    }
  }
    // 测试用
  // HAL_TIM_Base_Stop(&htim1);
  // HAL_TIM_Base_Stop(&htim2);
  // HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_1);
  // HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_2);
  // LL_TIM_SetAutoReload(TIM1, 65500);
  // LL_TIM_SetAutoReload(TIM2, 65500);
  // HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (uint32_t *)WaveSin, WAVE_SAMPLE_NUM, DAC_ALIGN_12B_R);
  // HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t *)WaveSin, WAVE_SAMPLE_NUM, DAC_ALIGN_12B_R);
  // HAL_TIM_Base_Start(&htim1);
  // HAL_TIM_Base_Start(&htim2);
}

void WaveGenProcess(void)
{
  uint8_t i;
  TIM_TypeDef *TimPt;
  uint16_t Tick;
  for (i = 0; i < OSCILLATOR_MAX_NUM; i++)
  {
    if (WaveDitherFlag[i] == 1)
    {
      if (WavePitchTickBackup[i] != 0xFFFF)
      {
        if ((HAL_GetTick() - WaveGenDitherTick[i]) > DITHER_INTERVAL)
        {
          WaveGenDitherTick[i] = HAL_GetTick();
          TimPt = (TIM_TypeDef *)TimBaseAddr[i];
          Tick = (uint16_t)(DitherCoefficient[i] * WavePitchTickBackup[WaveDitherCount[i]]);
          LL_TIM_SetAutoReload(TimPt, Tick);
          WaveDitherCount[i]++;
          if (WaveDitherCount[i] >= DITHER_COEFFICIENT_NUM)
          {
            WaveDitherCount[i] = 0;
          }
        }
      }
    }
  }
}

void WaveGenSetTimbre(uint8_t OSC, uint8_t Timbre)
{
  uint16_t i;
  uint16_t *WaveTimbreData;
  WaveTimbreFlag[OSC] = Timbre;
  WaveTimbreData = WaveTimbre[WaveTimbreFlag[OSC]];
  for (i = 0; i < WAVE_SAMPLE_NUM; i++)
  {
    WaveOutBuf[OSC][i] = WaveTimbreData[i] / (VOLUME_MAX_NUM - WaveVolumeCount[OSC]);
  }
}

void WaveGenSetDither(uint8_t OSC, uint8_t Dither)
{
  WaveDitherFlag[OSC] = Dither;
}

void WaveGenSetPitch(uint8_t OSC, uint8_t Pitch)
{
  WavePitchFlag[OSC] = Pitch;
}

void WaveGenSetAmplitude(uint8_t OSC, uint8_t Amplitude)
{
  uint16_t i;
  uint16_t *WaveTimbreData;
  WaveVolumeCount[OSC] = Amplitude;
  WaveTimbreData = WaveTimbre[WaveTimbreFlag[OSC]];
  for (i = 0; i < WAVE_SAMPLE_NUM; i++)
  {
    WaveOutBuf[OSC][i] = WaveTimbreData[i] / (VOLUME_MAX_NUM - WaveVolumeCount[OSC]);
  }
}

void WaveGenDacPinCfg(uint8_t OSC, uint8_t Enable)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  /**DAC1 GPIO Configuration
  PA4     ------> DAC1_OUT1
  PA5     ------> DAC1_OUT2
  */
  GPIO_InitStruct.Pin = DacPin[OSC];
  if (Enable == 0)
  {
    HAL_GPIO_WritePin(GPIOA, DacPin[OSC], GPIO_PIN_RESET);
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  }
  else
  {
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  }
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

void WaveGenPitchStart(uint8_t OSC, uint8_t PitchNum)
{
  uint8_t PitchCount = 0;
  uint32_t Freq2Tick = 0;

  TIM_HandleTypeDef *TimHandlePt;
  DAC_HandleTypeDef *DacHandlePt;
  TIM_TypeDef *TimPt;
  uint32_t DacCh;

  PitchCount = PitchNum + WavePitchFlag[OSC] * 12;
  Freq2Tick = (64000000 / ToneTab[PitchCount]) / WAVE_SAMPLE_NUM;
  if (WavePitchTickBackup[OSC] != Freq2Tick)
  {
    if ((WavePitchTickBackup[OSC] == 0xFFFF))
    {
      WavePitchTickBackup[OSC] = Freq2Tick;
      TimPt = (TIM_TypeDef *)TimBaseAddr[OSC];
      TimHandlePt = TimHandle[OSC];
      DacHandlePt = DacHandle[OSC];
      DacCh = DacChannel[OSC];
      WaveGenDitherTick[OSC] = HAL_GetTick();
      WaveDitherCount[OSC] = 0;

      HAL_TIM_Base_Stop(TimHandlePt);
      HAL_DAC_Stop_DMA(DacHandlePt, DacCh);
      LL_TIM_SetAutoReload(TimPt, Freq2Tick);
      WaveGenDacPinCfg(OSC, 1);
      HAL_DAC_Start_DMA(DacHandlePt, DacCh, (uint32_t*)&WaveOutBuf[OSC][0], WAVE_SAMPLE_NUM, DAC_ALIGN_12B_R);
      HAL_TIM_Base_Start(TimHandlePt);
    }
    else
    {
      WavePitchTickBackup[OSC] = Freq2Tick;
      WavePitchTickNext[OSC] = Freq2Tick;
    }
  }
}

void WaveGenPitchStop(uint8_t OSC)
{
  //  TIM_HandleTypeDef *TimHandlePt;
  //  DAC_HandleTypeDef *DacHandlePt;
  //  uint32_t DacCh;

  //  TimHandlePt = TimHandle[OSC];
  //  DacHandlePt = DacHandle[OSC];
  //  DacCh = DacChannel[OSC];
  WavePitchTickBackup[OSC] = 0xFFFF;

  //  HAL_TIM_Base_Stop(TimHandlePt);
  //  HAL_DAC_Stop_DMA(DacHandlePt, DacCh);
  //  WaveGenDacPinCfg(OSC, 0);
}

void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
{
  // WaveGenSwitchTick[0] = HAL_GetTick();
  if (WavePitchTickBackup[0] == 0xFFFF)
  {
    HAL_TIM_Base_Stop(&htim1);
    HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_1);
    WaveGenDacPinCfg(0, 0);
  }
  if(WavePitchTickNext[0]!=0xFFFF)
  {
    HAL_TIM_Base_Stop(&htim1);
    HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_1);
    LL_TIM_SetAutoReload(TIM1, WavePitchTickNext[0]);
    HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_1, (uint32_t*)&WaveOutBuf[0][0], WAVE_SAMPLE_NUM, DAC_ALIGN_12B_R);
    HAL_TIM_Base_Start(&htim1);
    WavePitchTickNext[0] = 0xFFFF;
  }
}

void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
{
  // WaveGenSwitchTick[1] = HAL_GetTick();
  if (WavePitchTickBackup[1] == 0xFFFF)
  {
    HAL_TIM_Base_Stop(&htim3);
    HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_2);
    WaveGenDacPinCfg(1, 0);
  }
  if(WavePitchTickNext[1]!=0xFFFF)
  {
    HAL_TIM_Base_Stop(&htim3);
    HAL_DAC_Stop_DMA(&hdac1, DAC_CHANNEL_2);
    LL_TIM_SetAutoReload(TIM3, WavePitchTickNext[1]);
    HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t*)&WaveOutBuf[1][0], WAVE_SAMPLE_NUM, DAC_ALIGN_12B_R);
    HAL_TIM_Base_Start(&htim3);
    WavePitchTickNext[1] = 0xFFFF;
  }
}
